<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" :close-on-press-escape="mode != 'show' ? false : true"
             destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
			<el-form-item label="关联类型" prop="type">
				<el-select class="flex-1" v-model="form.type" placeholder="请选择关联类型">
					<el-option
							v-for="(item, index) in types"
							:key="index"
							:label="item.name"
							:value="item.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="关联名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入关联名称" />
			</el-form-item>
			<el-form-item label="关联模型" prop="model">
				<el-select class="flex-1" v-model="form.model" placeholder="请选择关联模型">
					<el-option
							v-for="item in models"
							:label="item"
							:value="item"
							:key="item"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="关联健" prop="local_key">
				<el-select
						class="flex-1"
						v-model="form.local_key"
						clearable
						placeholder="请选择关联健"
				>
					<el-option
							v-for="item in column"
							:key="item.id"
							:value="item.column_name"
							:label="`${item.column_name}：${item.column_comment}`"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="外键" prop="foreign_key">
				<el-input
						v-model="form.foreign_key"
						placeholder="关联表外键或中间表的外键"
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed','add','edit'],
	props: {
		types: { type: Array, default: () => [] },
		column: { type: Array, default:  () => [] },
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增关联',
				edit: '编辑关联',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id:"",
				name: '',
				model: '',
				type: '',
				local_key: '',
				foreign_key: ''
			},
			//验证规则
			rules: {
				name: [
					{
						required: true,
						message: '请输入关联名称'
					}
				],
				type: [
					{
						required: true,
						message: '请选择关联类型'
					}
				],
				model: [
					{
						required: true,
						message: '请选择关联模型'
					}
				],
				local_key: [
					{
						required: true,
						message: '请选择关联健'
					}
				],
				foreign_key: [
					{
						required: true,
						message: '请输入外键'
					}
				]
			},
			models:[],
		}
	},
	mounted() {
		this.getModels()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		async getModels() {
			var res = await this.$API.dev_tools.code.getModels.post();
			if (res.code == 1) {
				this.models = res.data
			}
		},
		//加载树数据
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					if (this.form.id) {
						this.$emit('edit',this.form)
					} else {
						this.$emit('add',this.form)
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
			this.form.type = data.type
			this.form.status = data.status
			this.form.remark = data.remark
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style></style>
